//
//  HYLStatusCell.swift
//  iOSwechat
//
//  Created by apple on 16/3/2.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

class HYLStatusCell: UITableViewCell {

    private var buttonTopViewConstraint: Constraint?
    
    // 构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 属性
    var status: HYLStatus? {
        didSet {
            // 当控制器给cell设置status模型时,将模型传递给 originalView
            originalView.status = status
            
            
            
            // 判断是否要显示 被转发微博内容
            buttonTopViewConstraint?.uninstall()
            if let retweeted_status = status?.retweeted_status {
            
                retweetView.hidden = false
                retweetView.status = retweeted_status
                bottonView.snp_makeConstraints(closure: { (make) -> Void in
                   self.buttonTopViewConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            } else {
                retweetView.hidden = true
                bottonView.snp_makeConstraints(closure: { (make) -> Void in
                    self.buttonTopViewConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        prepareUI()
    }
    

    func prepareUI() {
        
        // 1.添加子控件
        contentView .addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottonView)

        
        // 2.添加约束
        // 原创微博view约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            //make.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
        }
        
        // 被转发的微博内容
        retweetView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        // 底部按钮约束
        bottonView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
            self.buttonTopViewConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(bottonView)
        }
    }

    // MARK: - 懒加载
    // 原创微博cell
    private lazy var originalView: HYLStatusOriginalView = HYLStatusOriginalView()
    
    // 被转发的内容
    private lazy var retweetView: HYLStatusRetweetView = HYLStatusRetweetView()
    
    // 底部按钮
    private lazy var bottonView: HYLStatusBottonView = HYLStatusBottonView()

}
